package com.gr

import static org.junit.Assert.*

import java.util.Date;

import org.junit.*

class PersonIntegrationTests {
	
	// to store data in database
	static transactional = false
	
	// for automatic testing(rollback mechanism)
	//static transactional = true

    @Before
    void setUp() {
        // Setup logic here
    }

    @After
    void tearDown() {
        // Tear down logic here
    }

	/**
	 * requirement:	database with empty tables should be created
	 */
    @Test
    void testAllDB() {

		def company = new Company(title:"DU Organization", email:"duorgmain@duorg.com", startDate: new Date("2001/1/18")).save()
		
		def divA = new Division(title:"DivisionA", email:"divisiona@duorg.com", startDate:new Date("2011/3/21"), companyTo: company).save()
		def divB = new Division(title:"DivisionB", email:"divisionb@duorg.com", startDate:new Date("2010/1/11"), companyTo: company).save()
	
		def prA1 = new Project(title:"Project A1", email:"projectA1@duorg.com", startDate:new Date("8/18/2012"), divisionTo:divA).save()
		def prA2 = new Project(title:"Project A2", email:"projectA2@duorg.com", startDate:new Date("2011/2/12"), divisionTo:divA).save()
		def prB1 = new Project(title:"Project B1", email:"projectB1@duorg.com", startDate:new Date(), divisionTo:divB).save()
		def prB2 = new Project(title:"Project B2", email:"projectB2@duorg.com", startDate:new Date(), divisionTo:divB).save()
		def p1  = new Person(name: "John Gardner", email: "john.gardner@duorg.com", birthDate:new Date("1970/5/15"), hiredDate:new Date("2009/1/5"), projectTo:prA1).save()
		def p2  = new Person(name: "Bill Murrey", email: "bill.murrey@duorg.com", birthDate:new Date("1974/3/21"), hiredDate:new Date("2010/4/9"), projectTo:prA2).save()
		def p3  = new Person(name: "Sanny Goldberg", email: "sanny.goldberg@duorg.com", birthDate:new Date("1982/11/1"), hiredDate:new Date("2009/6/19"), projectTo:prB1).save()
		def p4  = new Person(name: "Simon Gunya", email: "simon.gunya@duorg.com", birthDate:new Date("1959/5/20"), hiredDate:new Date("2005/6/1"), projectTo:prA1).save()
		def p5  = new Person(name: "Siguny Uivers", email: "siguny.uivers@duorg.com", birthDate:new Date("1971/1/17"), hiredDate:new Date("2006/12/1"), projectTo:prA1).save()
		def p6  = new Person(name: "James Gosling", email: "james.gosling@duorg.com", birthDate:new Date("1979/7/30"), hiredDate:new Date("2011/3/5"), projectTo:prA2).save()
		def p7  = new Person(name: "Michael Sucsy", email: "michael.sucsy@duorg.com", birthDate:new Date("1959/5/20"), hiredDate:new Date("2005/6/1"), projectTo:prB1).save()
		def p8  = new Person(name: "Abby Kohn", email: "abby.kohn@duorg.com", birthDate:new Date("1959/5/20"), hiredDate:new Date("2005/6/1"), projectTo:prB2).save()
		def p9  = new Person(name: "Jason Katims", email: "jason.katims@duorg.com", birthDate:new Date("1959/5/20"), hiredDate:new Date("2005/6/1"), projectTo:prA1).save()
		def p10 = new Person(name: "Channing Tatum", email: "channing.tatum@duorg.com", birthDate:new Date("1959/5/20"), hiredDate:new Date("2005/6/1"), projectTo:prA1).save()
		def p11 = new Person(name: "Sam Neill", email: "sam.neill@duorg.com", birthDate:new Date("1959/5/20"), hiredDate:new Date("2005/6/1"), projectTo:prB1).save()
		def p12 = new Person(name: "Rachel McAdams", email: "rachel.mcAdams@duorg.com", birthDate:new Date("1959/5/20"), hiredDate:new Date("2005/6/1"), projectTo:prB2).save()
		
		def findCompany = Company.findByTitle("DU Organization")		
//		def findCompany2 = Company.get(company.id)		
//		def companyToFind = new Company(title:"DU Organization")
//		def findCompany3 = Company.find(companyToFind)
//		
//		assertEquals "DU Organization", findCompany.title
//		assertEquals 2, Company.get(findCompany.id).divisions.size()
		
		assertEquals 2, Division.list().size()	
		assertEquals 4, Project.list().size()
		assertEquals 12, Person.list().size()
		
		assertEquals 2, Division.count()
		assertEquals 4, Project.count()
		assertEquals 12, Person.count()
		
		assertEquals 2, Project.findAllByDivisionToLike(divA).size()
		assertEquals 2, Project.findAllByDivisionToLike(divB).size()		
    }
}